package com.example.demo.strategy;

/**
 * @author FENGYU
 * @description
 * @date 2023-12-09 10:23
 */
public class Context {
    private Strategy strategy;

    public Context(String name) {

        if ("add".equals(name)) {
            this.strategy = new OperationAdd();
        } else if ("sub".equals(name)) {
            this.strategy = new OperationSubtract();
        }

    }

    public Strategy getStrategy() {
        return strategy;
    }

    public void setStrategy(Strategy strategy) {
        this.strategy = strategy;
    }

    public int executeStrategy(int num1, int num2) {
        if (strategy ==null){
            throw new RuntimeException("不存在的策略");
        }
        return strategy.doOperation(num1, num2);
    }
}
